import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

import Layout from "@/views/layout/Layout";
import Login from "@/views/layout/Login";
const titleComment = window.ENV.systemName;
export const constantRouterMap = [
  {
    path: "",
    component: Layout,
    redirect: "home",
    children: [
      {
        path: "home",
        component: () => import("@/views/layout/components/home"),
        name: "home",
        meta: {
          title: titleComment,
          icon: "home",
          noCache: true,
          requireAuth: true
        }
      },
      {
        path: "aiSearch",
        component: () => import("@/views/AISearch/index.vue"),
        name: "aiSearch"
      },
      // {
      //   path: "dass/redirect_uri=:src",
      //   component: () => import("@/views/AISearch/index.vue"),
      //   name: "aiSearch"
      // },
      {
        path: "dass/redirect_uri=:src",
        component: () => import("@/views/layout/components/myIframe"),
        name: "dass",
        meta: {
          title: titleComment,
          icon: "dass",
          noCache: true
        }
      },
      {
        path: "error",
        component: () => import("@/views/layout/components/error"),
        name: "error",
        meta: {
          title: titleComment,
          icon: "error",
          noCache: true
        }
      },
      {
        path: "404",
        component: () => import("@/views/layout/components/404"),
        name: "404",
        meta: {
          title: titleComment,
          icon: "error",
          noCache: true
        }
      },
      {
        path: "500",
        component: () => import("@/views/layout/components/500"),
        name: "500",
        meta: {
          title: titleComment,
          icon: "error",
          noCache: true
        }
      }
    ]
  },
  {
    path: "login",
    component: Login,
    redirect: "login",
    children: [
      {
        path: "/login",
        component: () => import("@/views/login/index"),
        name: "/login",
        meta: {
          title: titleComment,
          icon: "login",
          noCache: true
        }
      }
    ]
  }
];

const router = new Router({
  mode: "history", // require service support
  base: process.env.BASE_URL,
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
});

router.beforeEach((to, from, next) => {
  let token = JSON.parse(localStorage.getItem("loginInfo"))
    ? JSON.parse(localStorage.getItem("loginInfo")).token
    : "";
  let title = titleComment;
  document.title = title;
  if (to.path === "/login") {
    next();
  } else {
    if (token === "null" || token === "") {
      next("/login");
    } else {
      next();
    }
  }
});

export default router;
